package tt.dz.order.util.page;

import java.util.Map;

import org.springframework.util.Assert;

/**
 * 默认的分页Director
 * 
 * @author anson-worker
 *
 * @param <T>
 */
public class DefaultPaginationDirector implements PaginationDirector {

	/**
	 * 分页构造器
	 */
	private PaginationBuilder builder;

	/**
	 * 业务sql
	 */
	private String businessSql;

	/**
	 * 对应参数
	 */
	private Map<String, Object> params;

	/**
	 * 当前第几页
	 */
	private int currentPageNo;

	/**
	 * 每页多少条数
	 */
	private int prePageSize;

	/**
	 * 显示列表column
	 */
	private String countSql;

	/**
	 * 
	 * @param builder
	 *            分页构造器
	 * @param countSql
	 *            查询条数sql
	 * @param businessSql
	 *            业务sql
	 * @param params
	 *            对应参数
	 * @param currentPageNo
	 *            当前第几页
	 * @param prePageSize
	 *            每页多少条数
	 */
	public DefaultPaginationDirector(PaginationBuilder builder, String countSql, String businessSql,
			Map<String, Object> params, int currentPageNo, int prePageSize) {
		super();
		this.builder = builder;
		this.countSql = countSql;
		this.businessSql = businessSql;
		this.params = params;
		this.currentPageNo = currentPageNo;
		this.prePageSize = prePageSize;
	}

	@Override
	public void setBuilder(PaginationBuilder builder) {
		this.builder = builder;
	}

	/**
	 * 创建
	 */
	@Override
	public <T> Pagination<T> construct(Class<T> clazz) {
		Assert.notNull(this.builder, "分页构建器不能为空");
		this.builder.doBusiness(this.countSql, this.businessSql, this.params);
		this.builder.doPagination(this.currentPageNo, this.prePageSize);
		return this.builder.product(clazz);
	}

}
